package com.itfreer.ui.dialog;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.itfreer.ui.R;

/**
 * 字典单选Adapter
 */
public class DomainRadioAdapter extends BaseAdapter {

    private Context context;
    private Map<Object, Object> domain;
    private String[] keySets;
    private HashMap<Object, RadioButton> radioMap = new HashMap<Object, RadioButton>();
    private Object value;

    public DomainRadioAdapter(Context context, Map<Object, Object> domain){
        this.context = context;
        this.domain = domain;

        if(domain!=null && domain.size()>0){
            keySets = new String[domain.size()];
            keySets = domain.keySet().toArray(keySets);
        }
    }

    @Override
    public int getCount() {
        if(keySets!=null){
            return keySets.length;
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if(keySets!=null){
            return keySets[position];
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置当前选择项
     * @param value
     */
    public void setCheckedValue(Object value){
        Set<Object> rKeys = radioMap.keySet();
        for (Object rKey : rKeys) {
            if (value != rKey) {
                radioMap.get(rKey).setChecked(false);
            }
        }

        if(radioMap.containsKey(value)){
            radioMap.get(value).setChecked(true);
        }

        this.value = value;
    }

    /**
     * 获取值
     * @return
     */
    public Object getCheckedValue(){
        return value;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.com_itfreer_core_ui_dialog_domaindialog_radio, null);

            RadioButton radio = (RadioButton)convertView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_radio_radiobutton);
            TextView textView = (TextView)convertView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_radio_textview);

            String text = domain.get(keySets[position]).toString();
            textView.setText(text);

            radio.setTag(keySets[position]);
            if(radioMap.containsKey(keySets[position])){
                radio.setChecked(radioMap.get(keySets[position]).isChecked());
            }
            radioMap.put(keySets[position], radio);
            radio.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Object vkey = v.getTag();
                    Set<Object> rKeys = radioMap.keySet();
                    for (Object rKey : rKeys) {
                        if (vkey != rKey) {
                            radioMap.get(rKey).setChecked(false);
                        }
                    }
                    value = vkey;
                }
            });
        }
        return convertView;
    }
}